<template>
  <base-layout
    :dialog-title="dialogTitle"
    :dialog-visible="dialogVisible"
    @dialog-close="dialogClose"
    :dialogWidth="dialogWidth"
  >
    <template #searchPanel>
      <el-row :gutter="20">
        <search-panel
          :showAddButton="true"
          @insert="addService"
          :addPer="'intention:gjSave'"
          addTitle="新增跟进"
        />
      </el-row>
    </template>
    <template #contentPanel>
      <data-table
        :value="tableData.data"
        :has-action="true"
        :table-column-attributes="tableColumnAttributes"
        :width="160"
        :isShowPage="false"
      >
        <template #action="{ row }">
          <i
            class="el-icon-edit"
            title="修改"
            v-permission="'intention:gjSave'"
            style="margin-left: 15px; cursor: pointer"
            @click="interestsInfo(row, 0)"
          ></i>
          <i
            class="el-icon-delete"
            title="删除"
            v-permission="'intention:gjDel'"
            style="color: red; margin-left: 15px; cursor: pointer"
            @click="handleDataDelete(row)"
          ></i>
        </template>
      </data-table>
    </template>
    <template #dialog>
      <form-create v-model="followUpFormModel" :rule="rule" :option="option" />
      <div slot="footer" class="dialog-footer text-center">
        <!-- <el-button @click="dialogVisible = false">取 消</el-button> -->
        <el-button
          type="primary"
          v-permission="'intention:gjSave'"
          @click="intentiongGJSave"
          >确 定</el-button
        >
      </div>
    </template>
  </base-layout>
</template>

<script>
import baseMixin from '@/mixins/base'
import { rulesForm } from './rules'
import { indexApis } from '@/api/pre-sale/intentionalCustomer'
import { CarModel } from '@/components/index' // 导入组件库
export default {
  mixins: [
    baseMixin({
      idName: 'id',
      dataName: [''],
      findData: indexApis.intentiongGJFindAll,
      deleteData: indexApis.intentiongGJDelete,
    }),
  ],
  data() {
    return {
      followUpFormModel: {},
      rule: rulesForm,
      option: {
        //显示表单重置按钮
        resetBtn: false,
        submitBtn: false,
      },
      dialogTitle: '客户跟进',
      dialogWidth: '40%',
      tableColumnAttributes: [
        { name: '意向车型', value: 'carModelName', type: 'text' },
        { name: '购车预算', value: 'budget', type: 'text' },
        { name: '跟进方式', value: 'mode', type: 'dict', dict: '跟进方式' },
        { name: '跟进结果', value: 'result', type: 'dict', dict: '跟进结果' },
        { name: '下次回访时间', value: 'returnTime', type: 'text' },
        { name: '创建人', value: 'createUserName', type: 'text' },
        { name: '创建时间', value: 'createTime', type: 'text' },
        { name: '备注', value: 'remark', type: 'text' },
      ],
    }
  },
  mounted() {
     this.searchOptions = {
          intentionId: this.id,
          type: 0,
        }
    
  },

  methods: {
    dialogClose() {
      this.dialogVisible = false
    },
    //新增
    addService() {
      this.dialogVisible = true

      this.$nextTick(async () => {
        await this.initForm()
         this.intentiongDetail()
      })
    },
    async intentiongDetail() {
      const { data, success } = await indexApis.intentiongDetail(this.id)
      if (success) {
       this.$nextTick(()=>{
          data.carModelId = [
        data.carBrandId,
        data.carSeriesId,
        data.timeType,
        data.carModelId,
      ]
      data.budget=data.carPrice
          this.followUpFormModel.setValue(data)
       
       })
      }
    },
    intentiongGJSave() {
      this.followUpFormModel.submit(async (formData) => {
        formData.intentionId = +this.id
        formData.carBrandId = formData.carModelId[0]
        formData.carSeriesId = formData.carModelId[1]
        // formData.timeType
        formData.carModelId = formData.carModelId[3]

        await indexApis.intentiongGJSave({
          ...this.currentRow,
          ...formData,
          type: 0,
        })

        this.setTableData()
        this.dialogClose()
      })
    },
    //修改
    async interestsInfo(row) {
      // this.currentRow=row;
      const { data } = await indexApis.intentiongGJDetail(row.id)
      this.addService()
      this.currentRow = data
      data.carModelId = [
        data.carBrandId,
        data.carSeriesId,
        data.timeType,
        data.carModelId,
      ]
      this.$nextTick(() => {
        this.followUpFormModel.setValue(data)
      })
    },

    //初始化
    initForm() {
      let formData = this.followUpFormModel.formData()
      for (let i in formData) {
        if (Array.isArray(formData[i])) {
          formData[i] = []
        } else {
          formData[i] = ''
        }
      }
      this.followUpFormModel.setValue(formData)
     
      setTimeout(() => {
        this.followUpFormModel.clearValidateState()
      }, 100)
    },
  },
}
</script>

<style lang="scss" scoped>
</style>